package com.bit.musicserver.Service;

import com.bit.musicserver.Tools.Constat;
import com.bit.musicserver.Tools.ResponseBodyMessage;
import com.bit.musicserver.mapper.LoveMusicMapper;
import com.bit.musicserver.model.Music;
import com.bit.musicserver.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class LoveMusicService {
    @Resource
    private LoveMusicMapper loveMusicMapper;
    /*收藏音乐*/
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam("id") String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        System.out.println("musicId:" + musicId);

        //1.检查是否登成功
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constat.USERINDFO_SESSION_KEY) == null ){
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1, "请登录后收藏", false);
        }
        //User userId = (User) session.getAttribute("USERINDFO_SESSION_KEY");
        List<User> users = (List) session.getAttribute("USERINDFO_SESSION_KEY");
        User userId = users.get(0);
        int userid = userId.getId();
        System.out.println("userid:" + userid);
        Music music  = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userid, musicId);
        if (music != null) {
            //=之前收藏过,不能在收藏了 TODO 这里可以拓展加一个取消收藏的功能
            return new ResponseBodyMessage<>(0,"收藏过此音乐",false);
        }
        Boolean effect = loveMusicMapper.insertLoveMusic(userid,musicId);
        if (effect) {
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
    }


    /*查找音乐*/
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request) {
        //1.检查是否登成功
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constat.USERINDFO_SESSION_KEY) == null ){
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"请登录后查找",null);
        }
        //User userId = (User) session.getAttribute("USERINDFO_SESSION_KEY");
        List<User> users = (List) session.getAttribute("USERINDFO_SESSION_KEY");
        User userid = users.get(0);
        int userId = userid.getId();
        System.out.println("userid:" + userid);

        List<Music> musicList = null;
        if(musicName == null){
            musicList = loveMusicMapper.findMusicByUserId(userId);
        }else {
            musicList = loveMusicMapper.findMusicBykeyAndUID(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查找到歌曲信息",musicList);
    }

    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam("id") String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        //1.检查是否登成功
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constat.USERINDFO_SESSION_KEY) == null ){
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"请登录后取消收藏",null);
        }
        //User userId = (User) session.getAttribute("USERINDFO_SESSION_KEY");
        List<User> users = (List) session.getAttribute("USERINDFO_SESSION_KEY");
        User userid = users.get(0);
        int userId = userid.getId();

        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(ret==1){
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }
        return new ResponseBodyMessage<>(-1,"取消收藏失败",true);
    }
}
